﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vibz.TeamAssignment.Common.Entity;

namespace Vibz.TeamAssignment
{
    internal partial class TaskFlow :Form
    {
        AssignmentClient _aClient;
        Task _task;
        public TaskFlow(AssignmentClient client, Task task)
        {
            InitializeComponent();
            _aClient = client;
            _task = task;
            LoadTaskFlow();
        }
        void LoadTaskFlow()
        {
            List<Task> taskList= _aClient.GetAllSubTask(_task.TaskID);
            TreeNode node = new TreeNode(_task.Title + "- Status:" + _task.Status.ToString() 
                + (_task.Status != TaskStatus.Close ? ", Due date:" + _task.DueDate : ""), 
                LoadSubTask(_task.TaskID, taskList));
            node.BackColor = (_task.Status == TaskStatus.Close
                ? Color.LightGray
                : (_task.DueDate.CompareTo(DateTime.Today) < 0
                    ? Color.LightPink
                    : Color.LightGreen));
            tvTaskFlow.Nodes.Add(node);
            node.ToolTipText = "Created By: " + _task.CreatedBy + "\r\n" + _aClient.RevertRTF(_task.TaskDescription);
            tvTaskFlow.ExpandAll();
            tvTaskFlow.ShowNodeToolTips = true;
        }
        TreeNode[] LoadSubTask(int taskId, List<Task> taskList)
        {
            TreeNode[] retValue = (from task in taskList
                                   where task.ParentID == taskId
                                   select new TreeNode(task.Title + "- Status: " + task.Status.ToString()
                + (task.Status != TaskStatus.Close ? ",  Due date: " + task.DueDate : ""))
                                   {
                                       Tag = task,
                                       BackColor = (task.Status == TaskStatus.Close
                                            ? Color.LightGray
                                            : (task.DueDate.CompareTo(DateTime.Today) < 0
                                                ? Color.LightPink
                                                : Color.LightGreen)),
                                       ToolTipText = "Created By: " + _task.CreatedBy
                                            + "\r\n" + _aClient.RevertRTF(_task.TaskDescription)
                                   }).ToArray();
            foreach(TreeNode tn in retValue)
            {
                tn.Nodes.AddRange(LoadSubTask(((Task)tn.Tag).TaskID, taskList));
            }
            return retValue;
        }
    }
}
